package com.wing.oauth2.filter;

import com.wing.common.constant.ClientId;
import com.wing.oauth2.exception.VersionException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码过滤器
 *
 * @author
 */
//@Profile({"test","pro"})
@Component
public class VersionFilter extends OncePerRequestFilter {



    @Lazy
    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Value("${version.cab_version}")
    private String cabVersion;
    @Value("${version.test_enterpriseCode}")
    private String testEnterpriseCode;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
//        logger.info(httpServletRequest.getRequestURI());
//        logger.info(httpServletRequest.getMethod());
//        logger.info(httpServletRequest.getRemoteHost());
        String clientId = httpServletRequest.getParameter("client_id");
        String version=httpServletRequest.getParameter("version");
        String enterpriseCode=httpServletRequest.getParameter("enterpriseCode");


        if ("/oauth2/admin/user/login".equalsIgnoreCase(httpServletRequest.getRequestURI()) && httpServletRequest.getMethod().equals("POST")) {

                if (StringUtils.isNotBlank(clientId)){

                    if (ClientId.CLIENT_ID_CAB_APP.equals(clientId)&&(version==null||cabVersion.compareToIgnoreCase(version)>0)){
                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new VersionException("版本不是最新的，请更新最新的版本"));
                        return;
                    }
                    if(StringUtils.isNotBlank(testEnterpriseCode) && StringUtils.isNotBlank(enterpriseCode) && !enterpriseCode.equalsIgnoreCase(testEnterpriseCode)){
                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new VersionException("服务器升级维护中,请稍后更新版本在使用!"));
                        return;
                    }
//
//                    if (ClientId.CLIENT_ID_WCAB_APP.equals(clientId)&&!GlobalConstants.WCAB_VERSION.equalsIgnoreCase(version)){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new VersionException("版本不是最新的，请更新最新的版本"));
//                        return;
//                    }
                }

        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }



}